/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Model.Comentarios;
import Model.User;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author pedro
 */
public class UsuarioUtil {

     public static String urlServer=ConfUtil.url;

    public static ArrayList<User> getUsuario() throws JSONException {


        Gson g = new Gson();
        User user = new User();
        ArrayList<User> users = new ArrayList();
        JSONArray values = new JSONArray();

        try {
            URL url = new URL(urlServer+"usuarios");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "text/plain");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;

            Type collectionType = new TypeToken<List<User>>() {
            }.getType();
            while ((output = br.readLine()) != null) {

                users = g.fromJson(output, collectionType);

            }

            conn.disconnect();




        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return users;

    }
    
      public static List<User> buscaUsuarios(User cadastro) {
        Gson g = new Gson();
        List<User> usuarios = null;  
        String input = g.toJson(cadastro);
        System.out.println(input);
        try {

            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg = null;
            boolean loggedIn = false;

            URL url = new URL(urlServer+"usuarios");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            Type collectionType = new TypeToken<List<User>>() {
            }.getType();
            while ((output = br.readLine()) != null) {
                usuarios = g.fromJson(output, collectionType);
            }
            
            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
        return usuarios;
    }
}
